package cn.com.surker.workcon.service.xugong;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.common.MessageCenterException;
import cn.com.surker.enums.production.invoice.InvoiceSendTypeEnum;
import cn.com.surker.lab.dao.LabExpendUploadDao;
import cn.com.surker.lab.entity.LabExpendUpload;
import cn.com.surker.production.dao.ProductionIndentDao;
import cn.com.surker.production.entity.ProductionIndentDispatch;
import cn.com.surker.production.entity.vo.ProductionIndentVo;
import cn.com.surker.util.StringUtils;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.ProduceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * 派单实现接口
 *
 * @author luotao
 * @date 2024/12/18
 */
@Slf4j
public class ProduceServiceImpl extends GKJDBCService implements ProduceService {


	@Resource
	private ProductionIndentDao indentDao;
	@Resource
	private LabExpendUploadDao labUploadDao;


	@Value("${erp.datasource.erp.db}")
	private String erpDb;

	@Override
	public int insert(Long lineId, ProductionIndentDispatch dispatch) {
		if (lineId == null || dispatch == null) {
			return -1;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return -1;
		}
		//	任务单编号
		String indentCode = dispatch.getIndentCode();
		if (StringUtils.isBlank(indentCode)) {
			throw new MessageCenterException("派单" + dispatch.getCode() + "没有传入任务单编号数据");
		}
		Long indentId = dispatch.getIndentId();
		//	查询任务单数据
		ProductionIndentVo indentVo = indentDao.queryFullById(erpDb, indentId);
		if (indentVo == null) {
			throw new MessageCenterException("未查询出正确的任务单信息:(indentId)");
		}
		Integer uploadType = dispatch.getUploadType();
		if (InvoiceSendTypeEnum.SAND.getCode().equals(uploadType)) {
			indentCode = "R" + indentCode;
		}
		//	查询配合比数据
		List<LabExpendUpload> labExpendUploads = labUploadDao.queryByIndentId(erpDb, indentVo.getOrgId(), indentId, lineId);
		if (labExpendUploads == null || labExpendUploads.isEmpty()) {
			throw new MessageCenterException("派单未查询出配合比数据");
		}
		//	配合比编号
		String labLevelCode = null;
		for (LabExpendUpload expendUpload : labExpendUploads) {
			Long uploadLine = expendUpload.getUploadLine();
			if (lineId.equals(uploadLine)) {
				//	判断是否上传
				Integer isUpload = expendUpload.getIsUpload();
				if (isUpload != null && isUpload.equals(1)) {
					//	表示已经上传,获取配合比编号
					labLevelCode = expendUpload.getCode();
					break;
				}
			}
		}
		if (StringUtils.isBlank(labLevelCode)) {
			//	没有配合比编号
			throw new MessageCenterException("没有查询出上传的配合比编号,请重新上传配合比");
		}

		//	开启事务
		beginTransaction(lineId);

		return 1;
	}

	public Integer queryByDispatchId(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return null;
		}

		return 1;
	}

	@Override
	public Boolean disHasDel(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return null;
		}
		return false;
	}

	@Override
	public Integer delDispatch(Long lineId, Long dispatchId) {
		//	开启事务
		beginTransaction(lineId);
		return 1;
	}
}


